function reallocSimulatedObject(p,nold,n)
  use derived,only:SimulatedObject
  implicit none
  type(SimulatedObject),pointer,dimension(:) :: &
       reallocSimulatedObject,p
  integer,intent(in) :: nold,n

  integer :: i,j,ierr

  allocate(reallocSimulatedObject(1:n),stat=ierr)

  if (ierr.ne.0) call abnormal_stop("reallocSimulatedObject",&
       "Memory allocation causes an error.")

  do i=1,n
     reallocSimulatedObject(i)%n_event=0
     reallocSimulatedObject(i)%n_alloc_event=0
     nullify(reallocSimulatedObject(i)%event_table_list)
  end do

  if (.not.associated(p)) return

  do i=1,nold
     reallocSimulatedObject(i)%index=p(i)%index
     reallocSimulatedObject(i)%object_type=&
          p(i)%object_type
     do j=1,3
        reallocSimulatedObject(i)%pos(j)=p(i)%pos(j)
     end do
     do j=1,3
        reallocSimulatedObject(i)%vec(j)=p(i)%vec(j)
     end do
     do j=1,3
        reallocSimulatedObject(i)%box(j)=p(i)%box(j)
     end do
     reallocSimulatedObject(i)%vbox_index=&
          p(i)%vbox_index
     reallocSimulatedObject(i)%next=p(i)%next
     reallocSimulatedObject(i)%prev=p(i)%prev
     reallocSimulatedObject(i)%n_event=p(i)%n_event
     reallocSimulatedObject(i)%n_alloc_event=&
          p(i)%n_alloc_event
     reallocSimulatedObject(i)%removed=p(i)%removed
     if (p(i)%n_alloc_event.gt.0) then
        allocate(reallocSimulatedObject(i)%event_table_list(&
             p(i)%n_alloc_event))
        do j=1,p(i)%n_event
           reallocSimulatedObject(i)%event_table_list(j)=&
                p(i)%event_table_list(j)
        end do
        deallocate(p(i)%event_table_list)
     end if
  end do

  deallocate(p)

  return

end function reallocSimulatedObject
